package com.cap.dreamcircle.View.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cap.dreamcircle.Consts.Constants;
import com.cap.dreamcircle.Model.Bean.SolveDreamTalkEntity;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.GlideUtils;
import com.cap.dreamcircle.Utils.ViewHolderUtils;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by liucxu on 2017/9/15.
 */

public class AdapterSolveDreamTalk extends BaseAdapter {

    private Context context;
    private List<SolveDreamTalkEntity> dreamTalk=new ArrayList<>();

    public AdapterSolveDreamTalk(Context c,List<SolveDreamTalkEntity> datas){
        context=c;
        if(datas!=null){
            dreamTalk.clear();
            dreamTalk.addAll(datas);
        }
    }

    @Override
    public int getCount() {
        return dreamTalk.size();
    }

    @Override
    public Object getItem(int position) {
        return dreamTalk.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView=View.inflate(context, R.layout.adapter_solve_dream_item,null);
        }
        initViews(convertView,position);
        return convertView;
    }

    private void initViews(View convertView,int position){
        RelativeLayout layout_to_user= ViewHolderUtils.get(convertView,R.id.layout_to_user);
        CircleImageView to_user_header_view=ViewHolderUtils.get(convertView,R.id.to_user_header_view);
        TextView tv_to_user_talk_content=ViewHolderUtils.get(convertView,R.id.tv_to_user_talk_content);

        RelativeLayout layout_mine=ViewHolderUtils.get(convertView,R.id.layout_mine);
        CircleImageView mine_header_view=ViewHolderUtils.get(convertView,R.id.mine_header_view);
        TextView tv_mine_talk_content=ViewHolderUtils.get(convertView,R.id.tv_mine_talk_content);

        SolveDreamTalkEntity entity=dreamTalk.get(position);
        if(entity.isMyselfTalk()){
            layout_mine.setVisibility(View.VISIBLE);
            layout_to_user.setVisibility(View.GONE);
            GlideUtils.getInstance().LoadContextBitmap(context, Constants.BASE_URL+entity.getHeadImgUrl(),mine_header_view,R.mipmap.default_header,R.mipmap.default_header,GlideUtils.LOAD_BITMAP);
            tv_mine_talk_content.setText(entity.getContent());
        }
        else{
            layout_mine.setVisibility(View.GONE);
            layout_to_user.setVisibility(View.VISIBLE);
            GlideUtils.getInstance().LoadContextBitmap(context, Constants.BASE_URL+entity.getHeadImgUrl(),to_user_header_view,R.mipmap.default_header,R.mipmap.default_header,GlideUtils.LOAD_BITMAP);
            tv_to_user_talk_content.setText(entity.getContent());
        }
    }

    public void setDatas(List<SolveDreamTalkEntity> datas){
        if(datas!=null){
            dreamTalk.clear();
            dreamTalk.addAll(datas);
            notifyDataSetChanged();
        }
    }
}
